<?php
/**
 * Created by PhpStorm.
 * User: phperstar
 * Date: 2020/8/10
 * Time: 4:24 PM
 */
namespace Util\PHPExcel\Reader;

use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\ErrorCode;

use Util\PHPExcel\Reader\IReader;
use Util\PHPExcel\Reader\IReadFilter;


abstract class ReaderAbstract implements IReader
{
    protected $fileHandle = null;

    /**
     * Read data only?
     * Identifies whether the Reader should only read data values for cells, and ignore any formatting information;
     *        or whether it should read both data and formatting
     *
     * @var    boolean
     */
    protected $readDataOnly = false;

    /**
     * PHPExcel_Reader_IReadFilter instance
     *
     * @var IReadFilter
     */
    protected $readFilter;

    /**
     * Restrict which sheets should be loaded?
     * This property holds an array of worksheet names to be loaded. If null, then all worksheets will be loaded.
     *
     * @var array of string
     */
    protected $loadSheetsOnly;

    /**
     * Can the current PHPExcel_Reader_IReader read the file?
     *
     * @param     string         $pFilename
     * @return ResultWrapper
     */
    public function canRead($pFilename)
    {
        // Check if file exists
        $reuslt = $this->openFile($pFilename);
        if(!$reuslt->isSuccess()){
            return ResultWrapper::fail($reuslt->getData(), $reuslt->getErrorCode());
        }

        $readable = $this->isValidFormat();
        fclose($this->fileHandle);
        return ResultWrapper::success($readable);
    }

    /**
     * Open file for reading
     *
     * @param string $pFilename
     * @return ResultWrapper
     */
    protected function openFile($pFilename)
    {
        // Check if file exists
        if (!file_exists($pFilename) || !is_readable($pFilename)) {
            return ResultWrapper::fail('文件不存在',$pFilename, ErrorCode::$notAllowAccess);
        }

        // Open file
        $this->fileHandle = fopen($pFilename, 'r');
        if ($this->fileHandle === false) {
            return ResultWrapper::fail('读取文件失败',$pFilename, ErrorCode::$notAllowAccess);
        }
    }

    /**
     *  获取只读取数据模式配置
     *        If this is true, then the Reader will only read data values for cells, it will not read any formatting information.
     *        If false (the default) it will read data and formatting.
     *
     * @return    boolean
     */
    public function getReadDataOnly()
    {
        return $this->readDataOnly;
    }

    /**
     * 设置是否只读取数据
     *        Set to true, to advise the Reader only to read data values for cells, and to ignore any formatting information.
     *        Set to false (the default) to advise the Reader to read both data and formatting for cells.
     *
     * @param    boolean    $pValue
     *
     * @return    PHPExcel_Reader_IReader
     */
    public function setReadDataOnly($pValue = false)
    {
        $this->readDataOnly = $pValue;
        return $this;
    }

    /**
     * Scan theXML for use of <!ENTITY to prevent XXE/XEE attacks
     *
     * @param     string         $xml
     * @throws PHPExcel_Reader_Exception
     */
    public function securityScan($xml)
    {
        $pattern = '/\\0?' . implode('\\0?', str_split('<!DOCTYPE')) . '\\0?/';
        if (preg_match($pattern, $xml)) {
            throw new PHPExcel_Reader_Exception('Detected use of ENTITY in XML, spreadsheet file load() aborted to prevent XXE/XEE attacks');
        }
        return $xml;
    }

    /**
     * Read filter
     *
     * @return PHPExcel_Reader_IReadFilter
     */
    public function getReadFilter()
    {
        return $this->readFilter;
    }

    /**
     * Set read filter
     *
     * @param PHPExcel_Reader_IReadFilter $pValue
     * @return PHPExcel_Reader_IReader
     */
    public function setReadFilter(IReadFilter $pValue)
    {
        $this->readFilter = $pValue;
        return $this;
    }

    /**
     * Get which sheets to load
     * Returns either an array of worksheet names (the list of worksheets that should be loaded), or a null
     *        indicating that all worksheets in the workbook should be loaded.
     *
     * @return mixed
     */
    public function getLoadSheetsOnly()
    {
        return $this->loadSheetsOnly;
    }

    /**
     * Set which sheets to load
     *
     * @param mixed $value
     *        This should be either an array of worksheet names to be loaded, or a string containing a single worksheet name.
     *        If NULL, then it tells the Reader to read all worksheets in the workbook
     *
     * @return PHPExcel_Reader_IReader
     */
    public function setLoadSheetsOnly($value = null)
    {
        if ($value === null) {
            return $this->setLoadAllSheets();
        }

        $this->loadSheetsOnly = is_array($value) ? $value : array($value);
        return $this;
    }

    /**
     * Set all sheets to load
     *        Tells the Reader to load all worksheets from the workbook.
     *
     * @return PHPExcel_Reader_IReader
     */
    public function setLoadAllSheets()
    {
        $this->loadSheetsOnly = null;
        return $this;
    }

}